//
//  BroadcastCollectionViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "BroadcastCollectionViewCell.h"
#import "BroadcastDetailsCollectionViewCell.h"
#import "BroadcastCollectionReusableView.h"
#import "BroPaiHangCollectionViewCell.h"
#import "RecommandModel.h"
#import "TopRadioListModel.h"
@implementation BroadcastCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createCollectionView];
        [self getHandle];
        
        
    }
    
    
    return self;
    
    
}


- (void)getHandle {
    [WXCNetWorkTool getWithUrl:@"http://live.ximalaya.com/live-web/v1/getHomePageRadiosList?device=android" parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
        
        NSDictionary *resultDic = [result objectForKey:@"result"];
        NSArray *recommandRadioListArr = [resultDic objectForKey:@"recommandRadioList"];
        self.recommandRadioListArr = [NSMutableArray array];
        for (NSDictionary *dic in recommandRadioListArr) {
            
            RecommandModel *recModel = [[RecommandModel alloc] init];
            [recModel setValuesForKeysWithDictionary:dic];
            [self.recommandRadioListArr addObject:recModel];
            
        }
        
        NSArray *topRadioListArr = [resultDic objectForKey:@"topRadioList"];
        self.topRadioList = [NSMutableArray array];
        for (NSDictionary *dic in topRadioListArr) {
            TopRadioListModel *topModel = [[TopRadioListModel alloc] init];
            [topModel setValuesForKeysWithDictionary:dic];
            [self.topRadioList addObject:topModel];
            
        }
        [_collectionView reloadData];
        
    } fail:^(NSError *error) {
        
    }];
    
    
    
}



- (void)createCollectionView {
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    self.flowLayout.itemSize = CGSizeMake(self.width, 30);
//    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 5);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    self.flowLayout.headerReferenceSize = CGSizeMake(self.width, 20);
    [_collectionView registerClass:[BroadcastDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"BroadcastDetailsCollectionViewCell"];
    [_collectionView registerClass:[BroPaiHangCollectionViewCell class] forCellWithReuseIdentifier:@"BroPaiHangCollectionViewCell"];
    [_collectionView registerClass:[BroadcastCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
//        self.flowLayout.itemSize = CGSizeMake(self.width/3-10, 120);
        BroadcastDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BroadcastDetailsCollectionViewCell" forIndexPath:indexPath];
        RecommandModel *recModel = [self.recommandRadioListArr objectAtIndex:indexPath.row];
        [cell.topImageView sd_setImageWithURL:[NSURL URLWithString:recModel.picPath]];
        cell.label.text = recModel.rname;
         self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
//        cell.backgroundColor = [UIColor redColor];
        return cell;
        
    }
    else {
//        self.flowLayout.itemSize = CGSizeMake(self.width, 100);
        BroPaiHangCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BroPaiHangCollectionViewCell" forIndexPath:indexPath];
        TopRadioListModel *topModel = [self.topRadioList objectAtIndex:indexPath.row];
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:topModel.radioCoverSmall]];
        cell.programNameLabel.text = topModel.rname;
        cell.rnameLabel.text = [NSString stringWithFormat:@"直播中:  %@",topModel.programName];
//        NSInteger num = topModel.radioPlayCount.intValue / 100000;
//        NSLog(@"%ld",(long)num);
        cell.radioPlayCountLabel.text = [NSString stringWithFormat:@"收听人数:  %@万人",topModel.radioPlayCount.stringValue];
       
//        cell.backgroundColor = [UIColor greenColor];
        return cell;
    }
    
  
   
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return 3;
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        BroadcastCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            header.frame = CGRectMake(0, 0, self.width, 30);
            header.label.text = @"推荐电台";

        } else if (indexPath.section == 1){
            header.frame = CGRectMake(0, 0, self.width, 30);
            header.label.text = @"排行榜";

        }

        return header;
        
    } 
    return nil;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return CGSizeMake(self.width/3-10, 120);
        
    } else {
        
        return CGSizeMake(self.width, 100);
    }
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    [self.delegate passPage];
}


@end
